//
//  MyEarningsViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/30.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "MyEarningsViewController.h"
#import "MyEarningCell.h"
#import "MyearningRankCell.h"


@interface MyEarningsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, strong)UIButton * totalRankButton;

@property (nonatomic, strong)UIButton * monthRankButton;

@property (nonatomic, strong)UIView * leftLineView;

@property (nonatomic, strong)UIView * rightLineView;

@end

@implementation MyEarningsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
}

- (void)setupNavBar{
    
    self.title = @"我的收益";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    UIButton * rightButton = [[UIButton alloc] init];
    
    rightButton.frame = CGRectMake(0 , 0 , 75 * SCREENH6, 20 * SCREENH6);
    [rightButton setImage:[UIImage imageNamed:@"查看详情"] forState:UIControlStateNormal];
    [rightButton setTitle:@" 查看详情" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:13 * SCREENH6];
    [rightButton setTitleColor:ColorWithRGB(0xc8c8c8) forState:UIControlStateNormal]; //
    [rightButton addTarget:self action:@selector(pushToInformationVC) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * nextButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = nextButton;
}


- (void)setupUI{
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"chooseHeader"];
    
    [self.tableView registerClass:[MyEarningCell class] forCellReuseIdentifier:@"MyEarningCell"];
    
    [self.tableView registerClass:[MyearningRankCell class] forCellReuseIdentifier:@"rankCell"];
    
    [self.view addSubview:self.tableView];
    
    
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 2;
    }else{
        
        return 10;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
       
        MyEarningCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyEarningCell" forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            
            cell.nameLabel.text = @"总收益";
            cell.moneyLabel.text = @"$8888";
        }else{
            
            cell.nameLabel.text = @"本月收益";
            cell.moneyLabel.text = @"$2222";
        }
        
        return cell;
    }else{
        
        MyearningRankCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rankCell" forIndexPath:indexPath];
        
        cell.nameLabel.text = @"张三";
        cell.moneyLabel.text = @"+$8888";
        
        return cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 ) {
        
        return 60 * SCREENH6;
    }else{
        
        return 63 * SCREENH6;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 1 ) {
        
        return 55 * SCREENH6;
    }else{
        
        return 0;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"chooseHeader"];
    
    header.contentView.backgroundColor = ColorWithRGB(0x061a23);
    
    self.totalRankButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 185 * SCREENH6, 55 * SCREENH6)];
    
    self.totalRankButton.selected = YES;
    self.totalRankButton.backgroundColor = [UIColor clearColor];
    [self.totalRankButton setTitleColor:ColorWithRGB(0x3dba8a) forState:UIControlStateSelected];
    [self.totalRankButton setTitleColor:ColorWithRGB(0xc8c8c8) forState:UIControlStateNormal];
    self.totalRankButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    [self.totalRankButton addTarget:self action:@selector(didTotalRankButton) forControlEvents:UIControlEventTouchUpInside];
    [self.totalRankButton setTitle:@"总收益排行(前10)" forState:UIControlStateNormal];
    
    [header.contentView addSubview:self.totalRankButton];
    
//    [self.totalRankButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.top.bottom.mas_equalTo(header);
//        make.width.mas_equalTo(SCREEN_WIDTH / 2);
//    }];
    
    self.leftLineView = [[UIView alloc] init];    
    self.leftLineView.backgroundColor = ColorWithRGB(0x3dba8a);
    self.leftLineView.alpha = 1;
    
    [header.contentView addSubview:self.leftLineView];
    
    [self.leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.totalRankButton);
        make.bottom.mas_equalTo(header.contentView).offset(-1 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    
    self.monthRankButton = [UIButton new];
    
    self.monthRankButton.selected = NO;
    self.monthRankButton.backgroundColor = [UIColor clearColor];
    [self.monthRankButton setTitleColor:ColorWithRGB(0x3dba8a) forState:UIControlStateSelected];
    [self.monthRankButton setTitleColor:ColorWithRGB(0xc8c8c8) forState:UIControlStateNormal];
    self.monthRankButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    [self.monthRankButton addTarget:self action:@selector(didMonthRankButton) forControlEvents:UIControlEventTouchUpInside];
    [self.monthRankButton setTitle:@"本月收益排行(前10)" forState:UIControlStateNormal];
    
    [header addSubview:self.monthRankButton];
    
    [self.monthRankButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.bottom.mas_equalTo(header);
        make.width.mas_equalTo(SCREEN_WIDTH / 2);
    }];
    
    self.rightLineView = [UIView new];
    
    self.rightLineView.backgroundColor = [UIColor whiteColor];
    self.rightLineView.alpha = 0.1;
    
    [header addSubview:self.rightLineView];
    
    [self.rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.monthRankButton);
        make.bottom.mas_equalTo(header).offset(-1 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    return header;
}



- (void)didTotalRankButton{
    
    self.monthRankButton.selected = NO;
    self.totalRankButton.selected = YES;
    self.leftLineView.backgroundColor = ColorWithRGB(0x3dba8a);
    self.leftLineView.alpha = 1;
    self.rightLineView.backgroundColor = [UIColor whiteColor];
    self.rightLineView.alpha = 0.1;
    
}


- (void)didMonthRankButton{
    
    self.monthRankButton.selected = YES;
    self.totalRankButton.selected = NO;
    self.leftLineView.backgroundColor = [UIColor whiteColor];
    self.leftLineView.alpha = 0.1;
    self.rightLineView.backgroundColor = ColorWithRGB(0x3dba8a);
    self.rightLineView.alpha = 1;
    
}



- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)pushToInformationVC{
    
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
